/**
 * Created by huangxuehua on 2017/11/27.
 */
//$("header>div.goBack").click(function(){
//    var flag = nUtils.getUrlParam("flag");
//    if(flag == "myBuy"){
//        window.history.go(-1);
//    }else if(flag == "edit"){
//        window.history.go(-3);
//    }else if(flag == "mycenter"){
//        window.history.go(-1);
//    }else if(flag == "mycenter_edit"){
//        window.history.go(-3);
//    }
//
//});

var xpHome= new Vue({
    el:"#app",
    data: {
        addressList: [],//地址列表
    },
    methods: {
        listCheckBtn:function(idx){//设置用户默认地址
            var data={
                //"userid":userInfo.userid,
                "access_token":userInfo.access_token,
                "data[address_id]":xpHome.addressList[idx].address_id,//地址Id
                "data[is_def]":1//地址列表默认框
            }
            $.ajax({
                url:nUrls.setUserDefaultAddress,
                type:"POST",
                data:data,
                success:function(data){
                    //console.log("-+-+-+"+data)
                    var result = eval('(' + data + ')');
                    if (result && result.code == 200) {

                    }
                },
                error: function () {
                    console.log("默认地址出错");
                }
            })
        },
        editAddress: function (idx) {//编辑地址
            var flag = nUtils.getUrlParam("flag");
            if(flag == "myBuy") {
                window.location.href="./edit_address.html?flag=edit";
            } else if(flag="mycenter"){
                window.location.href="./edit_address.html?flag=mycenter_edit";
            }

            window.localStorage.setItem("editAddressInfo",JSON.stringify(xpHome.addressList[idx]))

        },

        goAddRegion:function(){//添加地址
            window.location.href = "../add_region.html";
        },
        getAddressList:function(){//获取用户地址列表
            var data = {userid:userInfo.userid};
            $.ajax({
                url: nUrls.getAddressListUrl,
                type: 'POST',
                data:data,
                success: function (data) {
                    var result = eval('(' + data + ')');
                    if (result && result.code == 200) {
                        xpHome.addressList = result.data;
                        //console.log("------",xpHome.addressList);
                    }
                },
                error: function () {
                    console.log("获取信息出错");
                }
            });
        },
        deleteBtn:function(idx){
            var dialog = new auiDialog();
            dialog.alert({
                title:"温馨提示",
                msg:'确定要删除此地址？',
                buttons:['取消','确定']
            },function(ret){
                if(ret.buttonIndex == 2){//确定
                    var data={
                        //"userid":userInfo.userid,
                        "access_token":userInfo.access_token,
                        "data[address_id]":xpHome.addressList[idx].address_id//地址Id
                    }
                    $.ajax({
                        url:nUrls.deleteAddress,
                        type:"POST",
                        data:data,
                        success:function(data){
                            var result = eval('(' + data + ')');
                            if (result && result.code == 200) {
                                // 删除地址栏
                                xpHome.addressList.splice(idx,1);//数组的删除
                            }
                        },
                        error: function () {
                            console.log("删除地址出错");
                        }
                    })

                }
            })
        },
        choseAddress:function(index){//点击选择地址
            var vv = xpHome.addressList[index];

            var flag = nUtils.getUrlParam("flag");
            if(flag == "myBuy"){
                window.localStorage.setItem("choseAddress",JSON.stringify(vv));
              window.history.go(-1);
            }else if(flag == "edit"){
                window.localStorage.setItem("choseAddress",JSON.stringify(vv));
                window.history.go(-3);
                //return;
            }else if(flag="mycenter"){
                return;
            }
        },

        goOnLine:function(){  //断网刷新
            nUtils.onLine();
        }
    }
});

$(function() {
    //ios 不刷新问题解决
    var isPageHide = false;
    window.addEventListener('pageshow', function () {
        if (isPageHide) {
            window.location.reload();//重新加载
        }
    });
    window.addEventListener('pagehide', function () {
        isPageHide = true;
    });
    xpHome.getAddressList();
})
$(function(){   //物理返回键
    // var flag = nUtils.getUrlParam("flag");
    //if(flag == "myBuy"){
    //    window.history.go(-1);
    //}else if(flag == "edit"){
    //    window.history.go(-3);
    //}else if(flag == "mycenter"){
    //    window.history.go(-1);
    //}else if(flag == "mycenter_edit"){
    //    window.history.go(-3);
    //}

});
